# 几何变换
# @Author ZhangGJ
# @Date 2021/11/25 11:49

import cv2.cv2 as cv2
import numpy as np


# 缩放
def zoom():
    img = cv2.imread('../images/demo.png')
    dst1 = cv2.resize(img, (100, 100))
    dst2 = cv2.resize(img, (400, 400))
    cv2.imshow('img', img)
    cv2.imshow('dst1', dst1)
    cv2.imshow('dst2', dst2)
    cv2.waitKey()
    cv2.destroyAllWindows()


def zoom2():
    img = cv2.imread('../images/demo.png')
    dst3 = cv2.resize(img, None, fx=1 / 3, fy=1 / 2)
    dst4 = cv2.resize(img, None, fx=2, fy=2)
    cv2.imshow('img', img)
    cv2.imshow('dst3', dst3)
    cv2.imshow('dst4', dst4)
    cv2.waitKey()
    cv2.destroyAllWindows()


# 翻转
def flip():
    img = cv2.imread('../images/demo.png')
    dst1 = cv2.flip(img, 0)
    dst2 = cv2.flip(img, 1)
    dst3 = cv2.flip(img, -1)
    cv2.imshow('img', img)
    cv2.imshow('dst1', dst1)
    cv2.imshow('dst2', dst2)
    cv2.imshow('dst3', dst3)
    cv2.waitKey()
    cv2.destroyAllWindows()


# 仿射变换
def warp_affine():
    img = cv2.imread('../images/demo.png')
    rows = len(img)
    cols = len(img[0])
    m = np.float32([[1, 0, 50],
                    [0, 1, 100]])
    dst = cv2.warpAffine(img, m, (cols, rows))
    cv2.imshow('img', img)
    cv2.imshow('dst', dst)
    cv2.waitKey()
    cv2.destroyAllWindows()


# 旋转
def span():
    img = cv2.imread('../images/demo.png')
    rows = len(img)
    cols = len(img[0])
    center = (rows / 2, cols / 2)
    m = cv2.getRotationMatrix2D(center, 30, 0.8)
    dst = cv2.warpAffine(img, m, (cols, rows))
    cv2.imshow('img', img)
    cv2.imshow('dst', dst)
    cv2.waitKey()
    cv2.destroyAllWindows()


# 右倾斜
def tilt_right():
    img = cv2.imread('../images/demo.png')
    rows = len(img)
    cols = len(img[0])
    p1 = np.zeros((3, 2), np.float32)
    p1[0] = [0, 0]
    p1[1] = [cols - 1, 0]
    p1[2] = [0, rows - 1]
    p2 = np.zeros((3, 2), np.float32)
    p2[0] = [50, 0]
    p2[1] = [cols - 1, 0]
    p2[2] = [0, rows - 1]
    m = cv2.getAffineTransform(p1, p2)
    dst = cv2.warpAffine(img, m, (cols, rows))
    cv2.imshow('img', img)
    cv2.imshow('dst', dst)
    cv2.waitKey()
    cv2.destroyAllWindows()


# 左倾斜
def tilt_left():
    img = cv2.imread('../images/demo.png')
    rows = len(img)
    cols = len(img[0])
    p1 = np.zeros((3, 2), np.float32)
    p1[0] = [0, 0]
    p1[1] = [cols - 1, 0]
    p1[2] = [0, rows - 1]
    p2 = np.zeros((3, 2), np.float32)
    p2[0] = [0, 0]
    p2[1] = [cols - 1 - 50, 0]
    p2[2] = [50, rows - 1]
    m = cv2.getAffineTransform(p1, p2)
    dst = cv2.warpAffine(img, m, (cols, rows))
    cv2.imshow('img', img)
    cv2.imshow('dst', dst)
    cv2.waitKey()
    cv2.destroyAllWindows()


# 透视
def perspective():
    img = cv2.imread('../images/demo.png')
    rows = len(img)
    cols = len(img[0])
    p1 = np.zeros((4, 2), np.float32)
    p1[0] = [0, 0]
    p1[1] = [cols - 1, 0]
    p1[2] = [0, rows - 1]
    p1[3] = [cols - 1, rows - 1]
    p2 = np.zeros((4, 2), np.float32)
    p2[0] = [90, 0]
    p2[1] = [cols - 90, 0]
    p2[2] = [0, rows - 1]
    p2[3] = [cols - 1, rows - 1]
    m = cv2.getPerspectiveTransform(p1, p2)
    dst = cv2.warpPerspective(img, m, (cols, rows))
    cv2.imshow('img', img)
    cv2.imshow('dst', dst)
    cv2.waitKey()
    cv2.destroyAllWindows()


if __name__ == '__main__':
    perspective()
